<template>
  <div>
    <el-button type="primary" @click="showAdd = true">添加检测数据</el-button>
    <el-table :data="checkList" style="width: 100%; margin-top: 20px;">
      <el-table-column prop="id" label="ID" width="60"/>
      <el-table-column prop="userName" label="用户"/>
      <el-table-column prop="data" label="检测数据"/>
      <el-table-column prop="date" label="检测时间"/>
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button size="small" @click="editCheck(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteCheck(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-model="showAdd" title="检测数据">
      <el-form :model="form">
        <el-form-item label="用户"><el-input v-model="form.userName"/></el-form-item>
        <el-form-item label="检测数据"><el-input v-model="form.data"/></el-form-item>
        <el-form-item label="检测时间"><el-date-picker v-model="form.date" type="date"/></el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showAdd = false">取消</el-button>
        <el-button type="primary" @click="saveCheck">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { getCheckListApi, addCheckApi, updateCheckApi, deleteCheckApi } from '../api/healthcheck'
const checkList = ref([])
const showAdd = ref(false)
const form = ref({})
const loadChecks = async () => { checkList.value = await getCheckListApi() }
const saveCheck = async () => {
  if(form.value.id) await updateCheckApi(form.value)
  else await addCheckApi(form.value)
  showAdd.value = false
  loadChecks()
}
const editCheck = (row) => { form.value = { ...row }; showAdd.value = true }
const deleteCheck = async (id) => { await deleteCheckApi(id); loadChecks() }
onMounted(loadChecks)
</script> 